package com.ht.medicaltreatment.fragment

import android.content.Intent
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseFragment
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.activity.SearchThingsActivity
import com.ht.medicaltreatment.activity.StockActivity
import com.ht.medicaltreatment.adapter.LcoationFragmentAdatper
import com.ht.medicaltreatment.bean.LocationBean
import com.ht.medicaltreatment.bean.OrderAllBean
import com.ht.medicaltreatment.viewmodel.LocationViewModel
import kotlinx.android.synthetic.main.fragment_all_layout.*

class LocationFragment : BaseFragment(), OnItemClickListener {

    private val model by viewModels<LocationViewModel>()

    override fun getLayoutResId() = R.layout.fragment_location_layout

    override fun init() {
        val adapter = LcoationFragmentAdatper()
        recyclerView.layoutManager = GridLayoutManager(requireContext(),3)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        showProgress()
        model.requestData({
            dismissProgress()
            it?.add(0,LocationBean(0,"",0,"","寻物"))
            adapter.setNewInstance(it)
        },{
            dismissProgress()
            ToastUtils.showShort(it)
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.data.get(position) as LocationBean
        if(item.iconPath == ""){
            startActivity(Intent(requireContext(),SearchThingsActivity::class.java))
        }else{
            startActivity(StockActivity.getIntent(requireContext(),item.id,StockActivity.FROM_LOCATION))
        }
    }

}
